package edu.cornell.cubooks.domain;

import java.io.Serializable;
import java.util.List;

import javax.persistence.*;
 
/**
 * Domain which holds information about authorities for the certain user.
 * 
 * @author CUBooks Team
 *
 */
@Entity
@Table(name="role")
public class Role implements Serializable {
 
	private static final long serialVersionUID = 6978168558172318987L;
	
	public static final Integer ROLE_BUYER = 1;
	public static final Integer ROLE_SELLER = 2;
	public static final String ROLE_BUYER_NAME = "ROLE_BUYER";
	public static final String ROLE_SELLER_NAME = "ROLE_SELLER";
	public static final String ROLE_ADMIN_NAME = "ROLE_ADMIN";
	
    @Id 
    @GeneratedValue
	private Integer id;
    private String name;
    
	@ManyToMany
	@JoinTable(name = "user_role",    
		joinColumns = { @JoinColumn(name = "role_id")},  
		inverseJoinColumns={@JoinColumn(name="user_id")})  
	private List<User> users;
	
	public Role() {
	}
	
	public Role(Integer id) {
		this.setId(id);
		// this.setName(name);
	}
	
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}
	
	public List<User> getUsers() {
		return users;
	}

	public void setUsers(List<User> users) {
		this.users = users;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	@Override
	public boolean equals(Object object) {
		if (object == null || !(object instanceof Role)) {
			return false;
		}
		Role role = (Role) object;
		if(getId().equals(role.getId())) {
			return true;
		}
		return false;
	}
	
	@Override
	public int hashCode() { 
		int hash = 1;
		hash = hash * 31 + id.hashCode();
		hash = hash * 31 + (name == null ? 0 : name.hashCode());
		return hash;
	}
	
}
